#!/bin/bash
#
# Ricardo Brito - ricardo[at]rbncurriculo.cjb.net 
# version 2
# Pode ser executado automaticamente fazendo 'crontab -e' e colocando (para rodar todos os dias as 3.30 da manha):
# SHELL=/bin/bash
# # Backup Data.fs do Zope
# 30 3 * * * /home/zope/bin/backup
# Baseado no script de Vincenzo Tozzi
#
echo proxy loading ...
export http_proxy=http://10.1.101.101:8080

#-----Variáveis editáveis ------------#
echo variables loading ...
HOME_PLONE=/opt/Plone-2.5.3
export PYTHONPATH=$HOME_PLONE/Python-2.4.4/lib/python2.4
PYTHON=$HOME_PLONE/Python-2.4.4/bin/python
ZOPEUTIL=$HOME_PLONE/bin
BKP_DIR=$HOME_PLONE/backups
ZEO_DIR=$HOME_PLONE/zeocluster/server
ZEO_PORT=8100
HOSTNAME=$(hostname -s)
SERVERNAME=10.1.5.11
USERBKP=brito
REMOTEDIR=backup
LOGFILE=message.log
#-------------------------------------#

echo -n "Checking log file ..."
[ -e $BKP_DIR/$LOGFILE ]; echo 'ok'

echo -n "Creating log file... "
echo $BKP_DIR/tempfile
echo ":: Relatorio de backup do Data.fs ($HOSTNAME) ::" | tee -a  $BKP_DIR/tempfile

echo Appending date in log file...
date | tee -a  $BKP_DIR/tempfile

echo locating and move olds backups files...|tee -a $BKP_DIR/tempfile
find $BKP_DIR/ -maxdepth 1 -type f -ctime +3 -name "*.deltafs" -o -name "*.fs" -o -name "*.dat" -exec mv -v {} $BKP_DIR/antigos \; 2>> $BKP_DIR/tempfile >> $BKP_DIR/tempfile

echo 'Doing the pack ZEO...'| tee -a  $BKP_DIR/tempfile
#$PYTHON $ZOPEUTIL/zeopack.py -p $ZEO_PORT -d 7 -h localhost 2>> $BKP_DIR/tempfile >> $BKP_DIR/tempfile
$ZOPEUTIL/zeopack.py -h localhost -d 7 -p $ZEO_PORT 2>> $BKP_DIR/tempfile >> $BKP_DIR/tempfile
cat $BKP_DIR/tempfile | tee -a  $BKP_DIR/tempfile

echo 'Doing backup incremental Data.fs ...'| tee -a  $BKP_DIR/tempfile
#$PYTHON $ZOPEUTIL/repozo.py -Bvr $BKP_DIR -f $ZEO_DIR/var/Data.fs 2>> $BKP_DIR/tempfile >> $BKP_DIR/tempfile
$ZOPEUTIL/repozo.py -BQvr $BKP_DIR -f $ZEO_DIR/var/Data.fs 2>> $BKP_DIR/tempfile >> $BKP_DIR/tempfile

# Copia no servidor remoto
echo "Coping the backup into remote server ($SERVERNAME)..." | tee -a  $BKP_DIR/tempfile
scp $BKP_DIR/*.fs $USERBKP@$SERVERNAME:$REMOTEDIR/ 2>> $BKP_DIR/tempfile >> $BKP_DIR/tempfile
scp $BKP_DIR/*.deltafs $USERBKP@$SERVERNAME:$REMOTEDIR/ 2>> $BKP_DIR/tempfile >> $BKP_DIR/tempfile

echo "::`date` finish backup::" | tee -a  $BKP_DIR/$LOGFILE

echo -e "finish log file ...\n\n--------------\n\r" | tee -a  $BKP_DIR/tempfile

cat $BKP_DIR/tempfile | tee -a  $BKP_DIR/$LOGFILE
cat $BKP_DIR/tempfile | mailx -s 'Cron Job: backup PZP' root

rm $BKP_DIR/tempfile
